package com.java.dao.impl;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.stereotype.Repository;

import com.java.dao.MailDao;
import com.java.model.Mail;
import com.java.model.PageBean;
import com.java.util.AppConfig;
import com.java.util.MailUtil;
import com.java.util.StringUtil;

/**
 * department
 * 
 * @author ld
 *
 */
@Repository("mailDao")
public class MailDaoImpl implements MailDao {

	@Resource
	private JdbcTemplate jdbcTemplate;

	@Override
	public List<Mail> find(PageBean pageBean, Mail s_mail) {
		System.out.println("find:" + s_mail.toString());
		
		// 查询163邮箱列表
		boolean success = true;
		try {
			List<Mail> rmailList = MailUtil.receiveMail(AppConfig.rmailName, AppConfig.rpasswd);
			for (Mail mail : rmailList) {
				mail.setType("1");
				add(mail);
			}
			List<Mail> rmailList2 = MailUtil.receiveMail(AppConfig.smailName, AppConfig.spasswd);
			for (Mail mail : rmailList2) {
				mail.setType("1");
				add(mail);
			}
		} catch (IOException e) {
			System.out.println("接收邮件失败");
			e.printStackTrace();
			success = false;
		}
		if (success) {
			System.out.println("接收邮件成功");
		}
		
		StringBuffer sb = new StringBuffer("select * from t_mail");
		if (s_mail != null) {
			if (null != s_mail.getToAddress()) {
				sb.append(" and toAddress = '" + s_mail.getToAddress() + "'");
			}
			if (null != s_mail.getFromAddress()) {
				sb.append(" and fromAddress = '" + s_mail.getFromAddress() + "'");
			} 
			if (null != s_mail.getType()) {
				sb.append(" and type = '" + s_mail.getType() + "'");
			}

		}
		if (pageBean != null) {
			sb.append(" limit " + pageBean.getStart() + "," + pageBean.getPageSize());
		}
		final List<Mail> mailList = new ArrayList<Mail>();
		System.out.println(sb.toString().replaceFirst("and", "where"));
		jdbcTemplate.query(sb.toString().replaceFirst("and", "where"), new Object[] {}, new RowCallbackHandler() {

			@Override
			public void processRow(ResultSet rs) throws SQLException {
				Mail mail = new Mail();
				mail.setId(rs.getInt("id"));
				mail.setToAddress(rs.getString("toAddress"));
				mail.setFromAddress(rs.getString("fromAddress"));
				mail.setSendTime(rs.getString("sendTime"));
				mail.setContext(rs.getString("context"));
				mail.setType(rs.getString("type"));
				System.out.println(mail.toString());
				mailList.add(mail);
			}
		});
		return mailList;
	}

	@Override
	public int count(Mail s_mail) {
		StringBuffer sb = new StringBuffer("select count(*) from t_mail");
		if (s_mail != null) {
			if (StringUtil.isNotEmpty(s_mail.getToAddress())) {
				sb.append(" and toAddress = '" + s_mail.getToAddress() + "'");
			}
		}
		return jdbcTemplate.queryForObject(sb.toString().replaceFirst("and", "where"), Integer.class);
	}

	@Override
	public void add(Mail mail) {
		Mail fmail = loadByTime(mail.getSendTime());
		if (fmail.getId() == null) {
			String sql = "insert into t_mail values(null,?,?,?,?,?)";
			// ,mail.getType()
			jdbcTemplate.update(sql, new Object[] { mail.getToAddress(), mail.getFromAddress(), mail.getSendTime(),
					mail.getContext(), mail.getType() });
		}
	}

	@Override
	public Mail loadById(int id) {
		String sql = "select * from t_main where id=?";
		final Mail mail = new Mail();
		jdbcTemplate.query(sql, new Object[] { id }, new RowCallbackHandler() {
			@Override
			public void processRow(ResultSet rs) throws SQLException {
				mail.setId(rs.getInt("id"));
				mail.setToAddress(rs.getString("toAddress"));
				mail.setFromAddress(rs.getString("fromAddress"));
				mail.setSendTime(rs.getString("sendTime"));
				mail.setContext(rs.getString("context"));
				mail.setType(rs.getString("type"));
			}
		});
		return mail;
	}

	@Override
	public Mail loadByTime(String sendTime) {
		String sql = "select * from t_mail where sendTime=?";
		final Mail mail = new Mail();
		jdbcTemplate.query(sql, new Object[] { sendTime }, new RowCallbackHandler() {
			@Override
			public void processRow(ResultSet rs) throws SQLException {
				mail.setId(rs.getInt("id"));
			}
		});
		return mail;
	}

	@Override
	public void update(Mail mail) {
		System.out.println("未实现 mail update");
		// String sql = "update t_company set name=?,address=?,phone=?,remark=?
		// where id=?";
		// jdbcTemplate.update(sql, new Object[] { company.getName(),
		// company.getAddress(), company.getPhone(),
		// company.getRemark(), company.getId() });
	}

	@Override
	public void delete(int id) {
		System.out.println("未实现 mail delete");
		// String sql = "delete from t_company where id=?";
		// jdbcTemplate.update(sql, new Object[] { id });
	}

}
